package org.come.until;

import com.updateNew.MyIsif;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;

import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import javax.swing.plaf.metal.MetalSliderUI;

public class MySliderUI extends MetalSliderUI {

    Image image = new ImageIcon("skin/8100226/button/B89.png").getImage();
    Image image1 = new ImageIcon("inkImg/hongmu/1/43_png.button.lider$bar.png").getImage();

    /**
     * 绘制指示物
     */
    @Override
    public void paintThumb(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // g2d.setColor(Color.red);
        // 填充椭圆框为当前thumb位置
        // g2d.fillOval(thumbRect.x, thumbRect.y, thumbRect.width,
        // thumbRect.height);
        // 也可以帖图(利用鼠标事件转换image即可体现不同状态)
        thumbRect.setSize(13, 21);
        if("水墨UI".equals(MyIsif.getStyle())) {
            g2d.drawImage(image, thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height, null);
        }else{
            g2d.drawImage(image1, thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height, null);
        }
    }

    /**
     * 绘制刻度轨迹
     */
    @Override
    public void paintTrack(Graphics g) {
        int cy, cw;
        Rectangle trackBounds = trackRect;
        if (slider.getOrientation() == SwingConstants.HORIZONTAL) {
            Graphics2D g2 = (Graphics2D) g;
            cy = (trackBounds.height / 2) - 2;
            cw = trackBounds.width;

            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.translate(trackBounds.x, trackBounds.y + cy);

            // 设置背景颜色
            // g2.setPaint(new Color(31,11,15));
            g2.setPaint(new Color(0, 0, 0, 0));
            g2.fillRect(0, -cy, cw, cy * 2);

            int trackLeft = 0;

            int trackRight = 0;

            trackRight = trackRect.width - 1;

            int middleOfThumb = 0;

            int fillLeft = 0;

            int fillRight = 0;

            // 坐标换算
            middleOfThumb = thumbRect.x + (thumbRect.width / 2);
            middleOfThumb -= trackRect.x;

            if (!drawInverted()) {
                fillLeft = !slider.isEnabled() ? trackLeft : trackLeft + 1;
                fillRight = middleOfThumb;
            } else {
                fillLeft = middleOfThumb;
                fillRight = !slider.isEnabled() ? trackRight - 1 : trackRight - 2;
            }
            // 设定渐变
            if("水墨UI".equals(MyIsif.getStyle())) {
                g2.setPaint(new GradientPaint(0, 0, new Color(127, 176, 164), cw, 0, new Color(127, 176, 164), true));
            }else{
                g2.setPaint(new GradientPaint(0, 0, new Color(187,165,75), cw, 0, new Color(187,165,75), true));
            }
            g2.fillRect(0, -cy, fillRight - fillLeft, cy * 2);

            g2.setPaint(slider.getBackground());
            Polygon polygon = new Polygon();
            polygon.addPoint(0, cy);
            polygon.addPoint(0, -cy);
            polygon.addPoint(cw, -cy);
            g2.fillPolygon(polygon);
            polygon.reset();
            g2.setPaint(Color.WHITE);
            g2.drawLine(0, cy, cw - 1, cy);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            g2.translate(-trackBounds.x, -(trackBounds.y + cy));
        } else {
            super.paintTrack(g);
        }
    }

}
